package com.primaryedu.ui.app.parentsch;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.primaryedu.R;
import com.primaryedu.bean.CommentBean;
import com.primaryedu.data.Contants;
import com.primaryedu.ui.BaseActivity;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by thomas on 2018/8/28.
 */

public class LookingforparentsActivity extends BaseActivity implements View.OnClickListener {
    private WebView webView;
    private TextView text_look;
    private String urlStr = "";
    private String newId = "";
    List<CommentBean> list;
    private String type = "";

    private Handler handler = new Handler() {

        public void handleMessage(android.os.Message msg) {

            switch (msg.what) {
                case 1001:
                    break;

                default:
                    break;
            }
        }

        ;
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_lookingforparents);
    }

    @Override
    protected int BindLayout() {
        return R.layout.activity_lookingforparents;
    }

    @Override
    protected void initView() {
        Bundle bundle = this.getIntent().getBundleExtra("bundle"); //读取intent的数据给bundle对象

        urlStr = bundle.getString("urlStr"); //通过key得到value
//        int int1 = bundle.getInt("iff");
//        urlStr = getIntent().getStringExtra("urlStr");
        System.out.println("urlStr==" + urlStr);

        list = new ArrayList<>();
        ((TextView) findViewById(R.id.title)).setText("找家长");
        findViewById(R.id.back).setOnClickListener(this);
//		findViewById(R.id.text_write).setOnClickListener(this);
//		text_look.setOnClickListener(this);

        webView = (WebView) this.findViewById(R.id.webView);
        webView.loadUrl(urlStr);
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setDomStorageEnabled(true);
        settings.setAllowFileAccess(true);

        // 设置可以支持缩放
        settings.setSupportZoom(true);
        // 设置出现缩放工具
        settings.setBuiltInZoomControls(true);
        //扩大比例的缩放
        settings.setUseWideViewPort(true);
        //自适应屏幕
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        settings.setLoadWithOverviewMode(true);
        //覆盖WebView默认使用第三方或系统默认浏览器打开网页的行为，使网页用WebView打开
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                //返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
                view.loadUrl(url);
                return true;
            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onResume() {
        super.onResume();

//        getPageListJson() ;
        webView.loadUrl(urlStr);
    }

    @Override
    public void onClick(View v) {
        Intent intent = null;

        switch (v.getId()) {
            case R.id.back:
                finish();
                break;

            default:
                break;
        }
    }

    private void getPageListJson() {
        StringRequest stringRequest = new StringRequest(Request.Method.POST, Contants.getComment,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        System.out.println("getComment==" + response);
                        try {
                            JSONObject json = new JSONObject(response);
                            int result = json.getInt("result");
                            String reason = json.getString("reason");

                            if (result == 1) {
                                String data = json.getString("data");
                                JSONObject dataObj = new JSONObject(data);
                                String listStr = dataObj.getString("list");

                                list = new Gson().fromJson(listStr,
                                        new TypeToken<List<CommentBean>>() {
                                        }.getType());
                                handler.sendEmptyMessage(1001);
                                System.out.println("getComment=00=" + list.toString());

                            } else {
                                showToast(reason);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
            }
        }) {
            @Override
            protected Map<String, String> getParams() {
                //在这里设置需要post的参数
                Map<String, String> map = new HashMap<String, String>();
                map.put("objectid", newId);
                map.put("type", type);
                map.put("pageNo", "1");
                map.put("pageSize", "300");

                System.out.println("getComment=00=" + map.toString());
                return map;
            }
        };
        requestQueue.add(stringRequest);
    }
}
